package StringPackage;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class reverseString2_ {
  int count = 0;
  List<String> list = new ArrayList<>();
  List<List<String>> result = new ArrayList<>();

  @Test
  public void reverseString2Test(){
    String s = "abcdefg";
    System.out.println(reverseString2(s, 2));
  }

  public String reverseString2(String s,int k) {
    for (int i = 0 ; i < s.length() ; i += 2 * k){
      if (s.length() - i >=  k){
        s = reverse(s,i,i + k);
        continue;
      }
      s = reverse(s,i,s.length());
    }
    return s;
  }

  /*反转区间[start,end)之间的字符串s
  * start和end均表示索引*/
  public String reverse(String s,int start,int end){
    if (start < end){
      return s;
    }
    char[] chars = s.toCharArray();
    while(start < end){
      char temp = chars[end - 1];
      chars[end - 1] = chars[start];
      chars[start] = temp;
      start++;
      end--;
    }
    return chars.toString();
  }
}
